﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ToolExcelPub
{
    public static partial class ToolGlobal
    {
        /// <summary>
        /// 获取父目录
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        public static string GetParentFolder(string dir, int layer = 1)
        {
            string result = dir;
            try
            {
                for (int i = 0; i < layer; i++)
                {
                    result = Directory.GetParent(result).FullName;
                }
            }
            catch
            {
            }
            return result;
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filename"></param>
        public static void DeleteFile(string filename)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
        }
        /// <summary>
        /// 清空目录
        /// </summary>
        /// <param name="dir"></param>
        public static void EmptyFolder(string dir)
        {
            if (Directory.Exists(dir))
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                    {
                        File.Delete(d);
                    }
                    else
                    {
                        DeleteFolder(d);
                    }
                }
            }
        }
        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="dir"></param>
        public static void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir))
            {
                EmptyFolder(dir);
                Directory.Delete(dir);
            }
        }
        public static string OpenOldFile(string path)
        {
            FileStream fsr_class = new FileStream(path, FileMode.Open);
            StreamReader sr_class = new StreamReader(fsr_class, Encoding.Unicode);
            var stread = sr_class.ReadToEnd();
            sr_class.Close();
            return stread;
        }
        public static void WriteNewFile(string path, string text)
        {
            DeleteFile(path);
            FileStream fsJson = new FileStream(path, FileMode.OpenOrCreate);
            StreamWriter swJson = new StreamWriter(fsJson, new UTF8Encoding(false));
            swJson.Write(text);
            swJson.Close();
        }

        public static bool ToBool(this string str)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (int.TryParse(str, out var resultint))
            {
                return Convert.ToBoolean(resultint);
            }
            return bool.TryParse(str, out var result) ? result : false;
        }
        public static int ToInt(this string str)
        {
            int.TryParse(str, out int result);
            return result;
        }
        public static short ToShort(this string str)
        {
            short.TryParse(str, out short result);
            return result;
        }
        public static long ToLong(this string str)
        {
            long.TryParse(str, out long result);
            return result;
        }
        public static float ToFloat(this string str)
        {
            float.TryParse(str, out float result);
            return result;
        }
        public static double ToDouble(this string str)
        {
            double.TryParse(str, out double result);
            return result;
        }
        public static string[] Split(this string s, string separator)
        {
            if (string.IsNullOrEmpty(s))
                return new string[0];

            return s.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
        }
        public static List<string> ToListString(this string str, string strSeparator = "|")
        {
            string[] nodes = str.Split(strSeparator);
            return nodes.ToList();
        }
        public static List<List<string>> ToListListString(this string str, string strSeparator1 = "|", string strSeparator2 = "_")
        {
            string[] nodes1 = str.Split(strSeparator1);
            var result = new List<List<string>>();
            foreach (var nn in nodes1)
            {
                string[] nodes2 = nn.Split(strSeparator2);
                result.Add(nodes2.ToList());
            }
            return result;
        }
        public static List<int> ToListInt(this string str, string strSeparator = "|")
        {
            string[] nodes = str.Split(strSeparator);
            int[] nodesresult = Array.ConvertAll(nodes, delegate (string s) { return s.ToInt(); });
            return nodesresult.ToList();
        }
        public static List<List<int>> ToListListInt(this string str, string strSeparator1 = "|", string strSeparator2 = "_")
        {
            string[] nodes1 = str.Split(strSeparator1);
            var result = new List<List<int>>();
            foreach (var nn in nodes1)
            {
                string[] nodes2 = nn.Split(strSeparator2);
                int[] nodesresult = Array.ConvertAll(nodes2, delegate (string s) { return s.ToInt(); });
                result.Add(nodesresult.ToList());
            }
            return result;
        }
        public static List<float> ToListFloat(this string str, string strSeparator = "|")
        {
            string[] nodes = str.Split(strSeparator);
            float[] nodesresult = Array.ConvertAll(nodes, delegate (string s) { return s.ToFloat(); });
            return nodesresult.ToList();
        }
        public static List<List<float>> ToListListFloat(this string str, string strSeparator1 = "|", string strSeparator2 = "_")
        {
            string[] nodes1 = str.Split(strSeparator1);
            var result = new List<List<float>>();
            foreach (var nn in nodes1)
            {
                string[] nodes2 = nn.Split(strSeparator2);
                float[] nodesresult = Array.ConvertAll(nodes2, delegate (string s) { return s.ToFloat(); });
                result.Add(nodesresult.ToList());
            }
            return result;
        }
        public static List<double> ToListDouble(this string str, string strSeparator = "|")
        {
            string[] nodes = str.Split(strSeparator);
            double[] nodesresult = Array.ConvertAll(nodes, delegate (string s) { return s.ToDouble(); });
            return nodesresult.ToList();
        }
        public static List<List<double>> ToListListDouble(this string str, string strSeparator1 = "|", string strSeparator2 = "_")
        {
            string[] nodes1 = str.Split(strSeparator1);
            var result = new List<List<double>>();
            foreach (var nn in nodes1)
            {
                string[] nodes2 = nn.Split(strSeparator2);
                double[] nodesresult = Array.ConvertAll(nodes2, delegate (string s) { return s.ToDouble(); });
                result.Add(nodesresult.ToList());
            }
            return result;
        }
        public static Dictionary<string, string> ToDictionaryString(this string str, string strSeparator1 = "|", string strSeparator2 = "_", bool RemoveEmptyEntries = false)
        {
            var result = new Dictionary<string, string>();
            string[] nodes = str.Split(strSeparator1, (RemoveEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None));
            foreach (var node in nodes)
            {
                string[] nodes2 = node.Split(strSeparator2, (RemoveEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None));
                if (nodes2.Length > 1)
                {
                    result[nodes2[0]] = nodes2[1];
                }
            }
            return result;
        }
        public static Dictionary<int, int> ToDictionaryInt(this string str, string strSeparator1 = "|", string strSeparator2 = "_", bool RemoveEmptyEntries = false)
        {
            var result = new Dictionary<int, int>();
            string[] nodes = str.Split(strSeparator1, (RemoveEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None));
            foreach (var node in nodes)
            {
                string[] nodes2 = node.Split(strSeparator2, (RemoveEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None));
                if (nodes2.Length > 1)
                {
                    result[nodes2[0].ToInt()] = nodes2[1].ToInt();
                }
            }
            return result;
        }
        public static Dictionary<string, int> ToDictionaryStringInt(this string str, string strSeparator1 = "|", string strSeparator2 = "_", bool RemoveEmptyEntries = false)
        {
            var result = new Dictionary<string, int>();
            string[] nodes = str.Split(strSeparator1, (RemoveEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None));
            foreach (var node in nodes)
            {
                string[] nodes2 = node.Split(strSeparator2, (RemoveEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None));
                if (nodes2.Length > 1)
                {
                    result[nodes2[0]] = nodes2[1].ToInt();
                }
            }
            return result;
        }
        /// <summary>
        /// 获取两个字符串中间的内容
        /// </summary>
        /// <param name="sourse"></param>
        /// <param name="startstr"></param>
        /// <param name="endstr"></param>
        /// <returns></returns>
        public static string GetMidStr(this string sourse, string startstr, string endstr)
        {
            string result = string.Empty;
            int startindex, endindex;
            try
            {
                startindex = sourse.IndexOf(startstr);
                if (startindex == -1)
                    return result;
                string tmpstr = sourse.Substring(startindex + startstr.Length);
                endindex = tmpstr.IndexOf(endstr);
                if (endindex == -1)
                    return result;
                result = tmpstr.Remove(endindex);
            }
            catch
            {
            }
            return result;
        }
        //截取中间
        public static string MidStrEx_New(string sourse, string startstr, string endstr)
        {
            Regex rg = new Regex("(?<=(" + startstr + "))[.\\s\\S]*?(?=(" + endstr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
            return rg.Match(sourse).Value;
        }

    }
}
